package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public OverviewVO getOverview() {
        //1.查询redis缓存中的数据
        Object object = redisTemplate.opsForValue().get("overview");
        if (object != null) {
            log.info("从Redis中获取数据,并返回");
            return (OverviewVO) object;
        }

        //2.缓存中没有数据，则查询数据库
        //获取线索概览
        OverviewVO clueoverviewVO = clueMapper.getClueOverview();
        //获取商机概览
        OverviewVO businessoverviewVO = businessMapper.getBusinessReport();
        //封装OverviewVO
        BeanUtils.copyProperties(businessoverviewVO,clueoverviewVO,"clueTotal","clueWaitAllot","clueWaitFollow","clueFollowing","clueFalse","clueConvertBusiness");

        //4.缓存redis
        log.info("将线索和商机的概览数据,缓存到Redis中");
        redisTemplate.opsForValue().set("overview",clueoverviewVO,1, TimeUnit.MINUTES);
        return clueoverviewVO;
    }
}
